import { def } from "../common";
// 重写改变数组的七个方法，实现监听
const arrayProto = Array.prototype
export const arrayMethods = Object.create(arrayProto)
['push','pop', 'unshift', 'shift', 'reverse','sort','splice'].forEach(method => {
    const origin = arrayProto[method]
    def(arrayMethods, method, function mutator(...args){
        const result = origin.apply(this, args)
        const ob = this.__ob__
        let inserted
        switch (method) {
            case 'push':
            case 'unshift':
                inserted = args
                break;
        
            case 'splice':
                inserted = args.slice(2)
                break;
        }
        if(inserted){
            ob.observeArray(inserted)
        }
        ob.dep.notify()
        return result

    })
});